package com.cn.wanxi.util;

import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.*;
import java.util.List;

public class IO {

    private static File file = null;


    /**
     * 创建记事本
     *
     * @return
     */
    private static void createNote(String fileName) {

        File dir = new File(Path.PATH.getName());//文件夹
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File a = new File(dir, fileName);
        try {
            a.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        file = a;
    }

    public static String readNote() {
        StringBuilder aa = new StringBuilder();
        try {
//            InputStream inputStream = new FileInputStream(file);

//            inputStream.read();
//            int result = 0;
//            while ((result = inputStream.read()) != -1) {
////                返回的是字节  我们需要强制转换为字符
//                System.out.print((char) result);
//            }
            Reader reader = new FileReader(file);
            int result = 0;
//            StringBuilder和string和StringBuffer的区别？

            while ((result = reader.read()) != -1) {
//                返回的是字节  我们需要强制转换为字符
//                System.out.print((char) result);
//                这里需要字符串拼接
                aa.append((char) result);
            }
//            System.out.println(aa);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return aa.toString();
    }

    public static void writeNote(String fileName, List<String> list) {
        createNote(fileName);
        try {
//         所有的引用类型的父类都是   Object类
            OutputStream outputStream = new FileOutputStream(file);
            for (int i = 0; i < list.size(); i++) {
                outputStream.write(list.get(i).getBytes());
                outputStream.write(Path.SPLIT.getName().getBytes());
            }
//            String logo = companyDao.logo;
//            outputStream.write(logo.getBytes());
//            outputStream.write(SPLIT.getBytes());
////            链式调用
//            outputStream.write(companyDao.phone.getBytes());
//            outputStream.write(SPLIT.getBytes());
//            outputStream.write(companyDao.net.getBytes());
//            outputStream.write(SPLIT.getBytes());
//            outputStream.write(companyDao.email.getBytes());
//            outputStream.write(SPLIT.getBytes());
//            outputStream.write(companyDao.address.getBytes());
//            outputStream.write(IO.SPLIT.getBytes());
//            outputStream.write(companyDao.version.getBytes());
//            outputStream.write(IO.SPLIT.getBytes());
//            outputStream.write(companyDao.linePhone.getBytes());
//            outputStream.write(IO.SPLIT.getBytes());


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 递归
     *
     * @param file
     */
    public void recursion(File file) {

        File dir = new File(file.toString());//文件夹
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i]);
            if (files[i].isDirectory()) {
                recursion(files[i]);
            }
            files[i].delete();
        }
    }
}
